package org.as3search.providers {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	import flash.errors.IllegalOperationError;

	/**
	 * <code>ArraySearchProvider</code> provides iterator functionality
	 * for <code>Array</code> collections.
	 * 
	 * @see org.as3search.providers.SearchProvider
	 */
	public class ArraySearchProvider implements SearchProvider {
		
		private var _target:Array;
		
		private var currentIndex:Number = -1;
		/**
		 * Creates a new <code>ArraySearchProvider</code> instance.
		 * @param	target	The target <code>Array</code> for this instance to wrap. 
		 */
		public function ArraySearchProvider(target:Array = null) {
			this.target = target;
		}
		/**
		 * @inheritDoc
		 */
		public function hasNext():Boolean { 
			if ((currentIndex + 1) > (_target.length - 1)) 
				return false; 
			else 
				return true;
		}
		/**
		 * @inheritDoc
		 */
		public function next():Object { 
			if (!hasNext())
				throw new IllegalOperationError("There is no next item in the array!");
			
			currentIndex++;
			
			var returnValue:Object = _target[currentIndex];
			return returnValue;
		}
		/**
		 * @inheritDoc
		 */
		public function reset():void {
			this.currentIndex = -1;
		}
		/**
		 * @inheritDoc
		 */
		public function get target():Object { return _target; }
		public function set target(value:Object):void {
			if (!(value is Array) && value != null) {
				throw new ArgumentError("'target' must be of type Array");
			}
			
			_target = (value as Array);
		}
		/**
		 * @inheritDoc
		 */
		public function get length():Number { return _target == null ? 0 : _target.length; }
	}
}